package com.request.toyexchange;

/*
 * 发送申请交换
 * 用户需要选择用于交换的玩具，并填写自己的联系方式
 */

import android.R.anim;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bjtu.toyexchange.R;

public class SendRequest extends Activity {
	private Button fanhui, cho_toy, send_req;
	private TextView tv_username;
	private String username, phone;
	private EditText sen_way;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send_req);
		
		//得到用户名
		Intent intent = this.getIntent();
		username = intent.getStringExtra("username");
		
		//设置用户名textview为用户名
		tv_username = (TextView)SendRequest.this.findViewById(R.id.send_username);
		tv_username.setText(username);
		
		//选择交换玩具的按钮，点击后弹出选择框，选项为用户发布的玩具的名称
		cho_toy = (Button)findViewById(R.id.send_cho_toy);
		cho_toy.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final String[] own_toy = new String[]{"玩具1","玩具2","玩具3","玩具4","玩具5"};
				new AlertDialog.Builder(SendRequest.this).setTitle("选择玩具")
				.setIcon(android.R.drawable.ic_dialog_info)
				//设置自己发布的玩具
				.setSingleChoiceItems(own_toy, 0,
						new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								new AlertDialog.Builder(SendRequest.this)
								.setMessage("你选择的是：" + own_toy[which])
								.setNeutralButton("确定", new DialogInterface.OnClickListener() {
									
									@Override
									public void onClick(DialogInterface dialog, int which) {
										
									}
								}).show();
								dialog.dismiss();
								
							}
						}
				)
				.setNegativeButton("取消", null)
				.show();
			}
		});
		
		//发送申请的按钮
		send_req = (Button)findViewById(R.id.send_fabu_btn);
		send_req.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//得到用户输入的手机号码
				sen_way = (EditText)SendRequest.this.findViewById(R.id.send_way);
				phone = sen_way.getText().toString();
				
				//手机号码信息校验
				if (("").equals(phone) || null == phone) {
					Toast.makeText(SendRequest.this, "联系方式不能为空...",100).show();
					return;
				}
				else if (phone.length() != 11) {
					Toast.makeText(SendRequest.this,"请输入正确的手机号码...",100).show();
					return;
				}
				else {
					Toast.makeText(SendRequest.this, "发送玩具交换申请...", 200).show();
					SendRequest.this.finish();
				}
			}
		});
		
		//返回的按钮
		fanhui = (Button)findViewById(R.id.senreqback);
		fanhui.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SendRequest.this.finish();
			}
		});
		
	}
	
}
